package com.sjy.project.fym.api;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.sjy.framework.aspectj.lang.annotation.Log;
import com.sjy.framework.aspectj.lang.enums.BusinessType;
import com.sjy.project.fym.place.domain.FyPlace;
import com.sjy.project.fym.place.service.IFyPlaceService;
import com.sjy.project.fym.signin.domain.FyUserSignin;
import com.sjy.project.fym.signin.service.IFyUserSigninService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import com.sjy.framework.web.controller.BaseController;
import com.sjy.framework.web.domain.AjaxResult;
import com.sjy.common.constant.UserConstants;
import com.sjy.common.constant.UserContext;
import com.sjy.common.entity.SecuryUserDetail;
import com.sjy.common.result.BaseResponse;
import com.sjy.common.utils.TokenUtil;
import com.sjy.common.utils.poi.ExcelUtil;
import com.sjy.framework.web.page.TableDataInfo;

/**
 * 人员签到打卡记录Controller
 * 
 * @author sjy
 * @date 2022-04-18
 */
@RestController
@Api(value = "经开区防疫用户签到", description = "用户签到相关接口", tags = {"经开区防疫"})
@RequestMapping("/api/fym/signin")
public class ApiFyUserSigninController extends BaseController
{

    @Autowired
    private IFyUserSigninService fyUserSigninService;
    @Autowired
    private IFyPlaceService fyPlaceService;
    
    @Autowired
    private TokenUtil tokenUtil;

    //扫码打卡签到
    /**
     * 新增保存人员签到打卡记录
     * @throws Exception 
     */

    @Log(title = "人员签到打卡记录", businessType = BusinessType.INSERT)
    @ApiOperation(value = "201、用户微信扫核酸点二维码进行签到", position = 201, notes = "第一版只能在自己队伍所在的场所打卡；第二版可以任意扫码签到 2022年5月5日23:44:42")
    @ApiImplicitParams({
  	  @ApiImplicitParam(name = "placeId", value = "场所编号", required = false, example = "1"),
  	  @ApiImplicitParam(name = "lng", value = "签到经度", required = false, example = "120.1"),
  	  @ApiImplicitParam(name = "lat", value = "签到纬度", required = false, example = "27.1")
    })
    @PostMapping({"/signinByPlace"})
    public ResponseEntity<BaseResponse<String>> signinByPlace(Long placeId,BigDecimal lng,BigDecimal lat)
    { 
    	try {
    		SecuryUserDetail user = UserContext.getUserSession();
        	boolean flag = fyUserSigninService.signinByPlace(placeId,lng,lat,user);
        	if(flag) {
            	return BaseResponse.generateOKResponseEntity("签到成功","签到成功"); 
        	}
        	//重复签到特殊处理
        	return BaseResponse.generateBadResponseEntity("签到失败", placeId+user.getUserName());
		} catch (Exception e) {
			if("重复签到!".equals(e.getMessage())) {
				return BaseResponse.generateBadResponseEntity(999,e.getMessage(),null);
			}
			return BaseResponse.generateBadResponseEntity(e.getMessage(),null);
		}
    	
    }
    //获取用户一段时间范围内签到清单记录
    /**
     * 新增保存人员签到打卡记录
     * @throws Exception 
     */
    @ApiOperation(value = "202、获取当前用户打卡记录", position = 202, notes = "默认返回del_flag = 0的数据")
    @ApiImplicitParams({
  	  @ApiImplicitParam(name = "signinType", value = "签到类型 1上午 2下午", required = false, example = "1"),
  	  @ApiImplicitParam(name = "signinStatus", value = "签到状态 0成功 1失败", required = false, example = "0"),
  	  @ApiImplicitParam(name = "startTimeStr", value = "开始时间", required = false, example = "2022-04-18"),
  	  @ApiImplicitParam(name = "endTimeStr", value = "结束时间", required = false, example = "2022-04-19"),
  	  @ApiImplicitParam(name = "pageNum", value = "当前页", required = false, example = "1"),
  	  @ApiImplicitParam(name = "pageSize", value = "分页大小", required = false, example = "15"),
  	  @ApiImplicitParam(name = "orderByColumn", value = "排序字段", required = false, example = "create_time"),
  	  @ApiImplicitParam(name = "isAsc", value = "排序的方向desc或者asc", required = false, example = "asc"),
  	   
  	
    })
    @PostMapping({"/getSigninlistByUser"})
    public ResponseEntity<BaseResponse<TableDataInfo<FyUserSignin>>> getSigninlistByUser(String signinType,String signinStatus
    		,String startTimeStr,String endTimeStr) throws Exception
    { 
    	SecuryUserDetail user = UserContext.getUserSession();
    	startPage();
    	
    	FyUserSignin signin = new FyUserSignin();
    	Map<String,Object> params  = new HashMap<String,Object>();
    	params.put("startDateStr", startTimeStr);
    	params.put("endDateStr", endTimeStr);
    	signin.setParams(params);
    	signin.setUserId(user.getUserId());
    	signin.setSigninType(signinType);
    	signin.setSigninStatus(signinStatus);
    	signin.setDelFlag("0");
    	
        List<FyUserSignin> list = fyUserSigninService.selectFyUserSigninList(signin);
        return BaseResponse.generateOKResponseEntity(getDataTable(list)); 
    }
    
    @ApiOperation(value = "203、获取当前用户当前打卡所在场景", position = 203, notes = "")
    @ApiImplicitParams({
    })
    @PostMapping({"/getUserSigninPlace"})
    public ResponseEntity<BaseResponse<FyPlace>> getUserSigninPlace() throws Exception
    { 
    	SecuryUserDetail user = UserContext.getUserSession();
    	FyPlace fyPlace = fyPlaceService.selectFyPlaceBySigninUserId(user.getUserId());
    	if(fyPlace == null) {
    		return BaseResponse.generateBadResponseEntity("当前时段用户未签到!", null);
    	}
        return BaseResponse.generateOKResponseEntity(fyPlace); 
    }
}
